package com.example.loglib.Proxy;

import com.example.loglib.Constant;
import com.example.loglib.LogUtil;
import com.example.loglib.Thread.SingleThreadPool;

/**
 * Created by huangbei on 2017/8/28.
 */

//LogUtil代理
public class MyLog implements LogI{
    private AfterLog afterLog;
    private BeforeLog beforeLog;

    private LogI logI;

    private static MyLog instance;

    private MyLog(){
        logI = LogUtil.getInstance();
    }

    public static MyLog getInstance(){
        if(instance == null){
            synchronized (MyLog.class){
                if(instance == null){
                    instance = new MyLog();
                }
            }
        }
        return instance;
    }

    public void setBeforeLog(BeforeLog beforeLog){
        this.beforeLog = beforeLog;
    }

    public void setAfterLog(AfterLog afterLog){
        this.afterLog = afterLog;
    }

    /**********************ilog**************************/
    @Override
    public void ilog(final String tag, final String content) {
        if(isBeforeOrAfter()){
            logI.ilog(tag, content);
        }else {
            runbefore(tag, content);
            logI.ilog(tag, content);
            runAfter(tag, content);
        }
    }

    @Override
    public void ilog(final String content) {
        String tag = getClassName();
        ilog(tag,content);
    }

    /**********************elog**************************/
    @Override
    public void elog(final String tag, final String content) {
        if(isBeforeOrAfter()){
            logI.elog(tag,content);
        }else{
            runbefore(tag,content);
            logI.elog(tag,content);
            runAfter(tag,content);
        }
    }

    @Override
    public void elog(final String content) {
        String tag = getClassName();
        elog(tag,content);
    }

    /**********************wlog**************************/
    @Override
    public void wlog(final String tag, final String content) {
        if(isBeforeOrAfter()){
            logI.wlog(tag, content);
        }else {
            runbefore(tag, content);
            logI.wlog(tag, content);
            runAfter(tag, content);
        }
    }

    @Override
    public void wlog(final String content) {
        String tag = getClassName();
        wlog(tag,content);
    }

    private void runbefore(String tag,String content){
        if(beforeLog != null){
            beforeLog.before(tag,content);
        }
    }

    private void runAfter(String tag,String content){
        if(afterLog != null){
            afterLog.after(tag,content);
        }
    }

    //获取调用者的类名
    private String getClassName(){
        StackTraceElement stack[] = (new Throwable()).getStackTrace();
        for (int i = 0; i < stack.length; i++)
        {
            StackTraceElement s = stack[i];
            String name = s.getClassName();
            String method = s.getMethodName();
            String file = s.getFileName();
        }
        if(stack != null && stack.length > 2) {
            return stack[2].getClassName();
        }else{
            return "";
        }
    }

    //是否是runBefore或者runAfter里面调用的log
    private boolean isBeforeOrAfter(){
        StackTraceElement stack[] = (new Throwable()).getStackTrace();
        String method;
        for(int i=0;i< stack.length;i++){
            method = stack[i].getMethodName();
            if(method.equals(Constant.RUN_AFTER) || method.equals(Constant.RUN_BEFORE)){
                return true;
            }
        }
        return false;
    }

}
